<html>

	<head>
		<title>Ted Dobyns - Game Engine Construction: Homework 1 - Terrain and Lighting</title>

	<script id="shader-vs" type="x-shader/x-vertex">
		attribute vec3 aVertexPosition;
		attribute vec3 aVertexNormal;
		attribute vec2 aTextureCoord;
 
		uniform mat4 uMVMatrix;
		uniform mat4 uPMatrix;
		uniform mat4 uNMatrix;
		  
		uniform vec3 uAmbientColor;
		  
		uniform vec3 uLightingDirection;
		uniform vec3 uDirectionalColor;
		uniform vec3 uSpecularColor;
		uniform vec3 uCameraPos;

		uniform bool uEnableLighting;
		  
		varying vec3 vLightWeighting;
		varying vec2 vTextureCoord;
		varying vec3 vVertexPosition;
 
		void main(void)
		{
			gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);

			if (uEnableLighting)
			{
				vec4 transformedNormal = uNMatrix * normalize(vec4(aVertexNormal, 1.0));
				float directionalLightWeighting = max(dot(transformedNormal.xyz, uLightingDirection), 0.0);
		    
				vec4 point = uMVMatrix * vec4(aVertexPosition, 1.0);
				vec3 cameraVec = (uMVMatrix * vec4(uCameraPos, 1.0) - point).xyz;
				cameraVec = normalize(cameraVec);
				vec3 h = uLightingDirection + cameraVec;
				h = normalize(h);
				float specularLightWeighting = max(pow(dot(transformedNormal.xyz, h), 8.0), 0.0);
		    
				vLightWeighting = uAmbientColor + uDirectionalColor * directionalLightWeighting + uSpecularColor * specularLightWeighting;
			}
			else
			{
				vLightWeighting = vec3(1.0, 1.0, 1.0);
			}

			vTextureCoord = aTextureCoord;
			vVertexPosition = aVertexPosition;
		}
	</script>
	
	<script id="shader-fs" type="x-shader/x-fragment">
		#ifdef GL_ES
		precision highp float;
		#endif

		uniform sampler2D uTexSampler0;
		uniform sampler2D uTexSampler1;
		uniform sampler2D uTexSampler2;
		uniform sampler2D uTexSampler3;

		uniform float uMaxHeight;

		uniform bool uUseHeightTexturing;

		varying vec3 vLightWeighting;
		varying vec2 vTextureCoord;
		varying vec3 vVertexPosition;

		void main(void) 
		{
			vec4 texCol;
			
			if (uUseHeightTexturing)
			{
				vec4 texCol0 = texture2D(uTexSampler0, vec2(vTextureCoord.s, vTextureCoord.t));
				vec4 texCol1 = texture2D(uTexSampler1, vec2(vTextureCoord.s, vTextureCoord.t));
				vec4 texCol2 = texture2D(uTexSampler2, vec2(vTextureCoord.s, vTextureCoord.t));
				vec4 texCol3 = texture2D(uTexSampler3, vec2(vTextureCoord.s, vTextureCoord.t));

				//Scale the vertex position by the max height
				float vy = vVertexPosition.y / uMaxHeight;

				if (vy < 0.292)
					texCol = texCol0;
				else if (vy < 0.3125)
					texCol = mix(texCol0, texCol1, (vy - 0.292) / 0.021);
				else if (vy < 0.333)
					texCol = texCol1;
				else if (vy < 0.666)
					texCol = mix(texCol1, texCol2, (vy - 0.333) / 0.333); 
				else if (vy < 0.833)
					texCol = mix(texCol2, texCol3, (vy - 0.666) / 0.166);
				else
					texCol = texCol3;
			}
			else
			{
				texCol = texture2D(uTexSampler0, vec2(vTextureCoord.s, vTextureCoord.t));
			}

			gl_FragColor = vec4(vLightWeighting * texCol.rgb, texCol.a);
		}
	</script>

	<script type = "text/javascript" src = "sylvester.js"></script>
	<script type = "text/javascript" src = "glUtils.js"></script>
	<script type = "text/javascript" src = "objloader.js"></script>
	<script type = "text/javascript" src = "glLoader.js"></script>
	<script type = "text/javascript" src = "textureLoading.js"></script>
	<script type = "text/javascript" src = "BillboardVegetation.js"></script>
	<script type = "text/javascript" src = "keys.js"></script>
	<script type = "text/javascript" src = "camera.js"></script>
	<script type = "text/javascript" src = "terrain.js"></script>
	<script type = "text/javascript" src = "hud.js"></script>
	<script type = "text/javascript" src = "gameobject.js"></script>
	<script type = "text/javascript" src = "bullet.js"></script>
	<script type = "text/javascript" src = "enemy.js"></script>
	<script type = "text/javascript" src = "engine.js"></script>
	<script type = "text/javascript" src = "game.js"></script>
	
	
	</head>
	<body onload="gameStart();">
		<canvas id="glcanvas" style="border: none;" width="1024" height="768"></canvas>
		<canvas id="hudcanvas" style="border: none;" width="1024" height="768"></canvas>
		
		<table border = "1">
		<tr>
			<td><b>Controls:</b></td>
		</tr>
		<tr>
			<td>Forward/Backward</td> <td align = "right">W/S</td><br />
		</tr>
		<tr>
			<td>Strafe Left/Right</td> <td align = "right">A/D</td><br />
		</tr>
		<tr>
			<td>Turn Left/Right</td> <td align = "right">Left/Right</td><br />
		</tr>
		<tr>
			<td>Shoot</td> <td align = "right">Space</td><br />
		</tr>
		</table>
	</body>
</html>